﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace NRemedy.MVC.UI
{
    //public abstract class PermissionInputList
    //{
    //    public string User { get; set; }
    //    public abstract InputDisplayStatus Compute(object model, List<string> GroupList);
    //}

    //public abstract class PermissionControlList
    //{
    //    public string User { get; set; }

    //    public abstract ControlDisplayStatus Compute(object model, List<string> GroupList);
    //}

    //public abstract class PermissionObject<TModel>
    //{
    //    public List<String> GroupList { get; set; }

    //    public Expression<Func<TModel, bool>> Condition { get; set; }

    //    protected virtual bool ComputeConditon(object model){
    //        if(Condition == null) return true;
    //        var func = Condition.Compile();
    //        return func((TModel)model);
    //    }

    //    public virtual bool IsValid(object model, List<String> TargetGroupList)
    //    {
    //        bool ret = false;
    //        if (model == null) ret = true;
    //        ret = ComputeConditon(model);
    //        if (ret == false) return ret;
    //        if (GroupList == null || TargetGroupList == null) return ret;
    //        foreach (var g in GroupList)
    //        {
    //            if (TargetGroupList.Contains(g))
    //                return true;
    //        }
    //        return false;
    //    }

    //    public virtual bool IsValid(List<String> TargetGroupList)
    //    {
    //        if (GroupList == null || TargetGroupList == null) return true;
    //        foreach (var g in GroupList)
    //        {
    //            if (TargetGroupList.Contains(g))
    //                return true;
    //        }
    //        return false;
    //    }

    //}

    public class PermissionControlObject<TModel>
    {
        private Expression<Func<TModel, ControlDisplayStatus>> _expression;

        public PermissionControlObject(Expression<Func<TModel, ControlDisplayStatus>> expression)
        {
            _expression = expression;
        }

        public ControlDisplayStatus Compute(object model)
        {
            var func = _expression.Compile();
            return func((TModel)model);
        }
    }

    public class PermissionInputObject<TModel>
    {
        private Expression<Func<TModel, InputDisplayStatus>> _expression;

        public PermissionInputObject(Expression<Func<TModel, InputDisplayStatus>> expression)
        {
            _expression = expression;
        }

        public InputDisplayStatus Compute(object model)
        {
            var func = _expression.Compile();
            return func((TModel)model);
        }
    }

    public enum ControlDisplayStatus
    {
        Normal,
        Disabled,
        Hidden
    }

    public enum InputDisplayStatus
    {
        Normal,
        Disabled,
        Hidden
    }
}
